package com.hero.tweaks.widget;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Point;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.hero.tweaks.R;
import com.hero.tweaks.helpers.UrlImageViewHelper;
import com.hero.tweaks.util.Helpers;
import android.preference.Preference;

public class DeveloperPreference extends Preference {
	public static final String GRAVATAR_API = "http://www.gravatar.com/avatar/";
	public static int mDefaultAvatarSize = 200;
	public static int mLargeAvatarSize = 400;
	private ImageView photoView;
	private View barView;

	private TextView devName;

	private String nameDev;
	private String xdaThreadLink;
	private String devEmail;
	private final Display mDisplay;

	public DeveloperPreference(Context context, AttributeSet attrs) {
		super(context, attrs);

		TypedArray typedArray = null;
		try {
			typedArray = context.obtainStyledAttributes(attrs,
					R.styleable.DeveloperPreference);
			nameDev = typedArray
					.getString(R.styleable.DeveloperPreference_nameDev);
			typedArray
					.getString(R.styleable.DeveloperPreference_twitterHandle);
			typedArray
					.getString(R.styleable.DeveloperPreference_donateLink);
			typedArray
					.getString(R.styleable.DeveloperPreference_xdaLink);
			xdaThreadLink = typedArray
					.getString(R.styleable.DeveloperPreference_xdaThreadLink);
			devEmail = typedArray
					.getString(R.styleable.DeveloperPreference_emailDev);
		} finally {
			if (typedArray != null) {
				typedArray.recycle();
			}
		}
		WindowManager wm = (WindowManager) getContext().getSystemService(
				Context.WINDOW_SERVICE);
		mDisplay = wm.getDefaultDisplay();
	}

	public String getGravatarUrl(String email) {
		Point point = new Point();
		mDisplay.getSize(point);
		mDefaultAvatarSize = point.x;
		return null;
	}

	public String getGravatarUrlLarge(String email) {
		Point point = new Point();
		mDisplay.getSize(point);
		mDefaultAvatarSize = point.x;
		return null;
	}

	@Override
	protected void onBindView(View view) {
		super.onBindView(view);
		if (xdaThreadLink != null) {
			final OnPreferenceClickListener openXDAThread = new OnPreferenceClickListener() {
				@Override
				public boolean onPreferenceClick(Preference preference) {
					Uri xdaThreadURL = Uri.parse(xdaThreadLink);
					final Intent intent = new Intent(Intent.ACTION_VIEW,
							xdaThreadURL);
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
							| Intent.FLAG_ACTIVITY_CLEAR_TOP);
					getContext().startActivity(intent);
					return true;
				}
			};
			if (Helpers.getROMVersion().contains("Renovate")) {
				this.setOnPreferenceClickListener(openXDAThread);
				UrlImageViewHelper.setUrlDrawable(this.photoView,
						getGravatarUrl(devEmail), R.drawable.renovate,
						UrlImageViewHelper.CACHE_DURATION_ONE_WEEK);
				barView.setVisibility(View.GONE);
			}
		} else if (Helpers.getROMVersion().contains("Renovate")) {
			UrlImageViewHelper.setUrlDrawable(this.photoView,
					getGravatarUrl(devEmail), R.drawable.renovate,
					UrlImageViewHelper.CACHE_DURATION_ONE_WEEK);
			barView.setVisibility(View.GONE);
		}
		devName.setText(nameDev);
	}

	@Override
	protected View onCreateView(ViewGroup parent) {
		super.onCreateView(parent);
		View layout = View.inflate(getContext(), R.layout.clickable_header,
				null);
		devName = (TextView) layout.findViewById(R.id.name);
		photoView = (ImageView) layout.findViewById(R.id.photo);
		barView = layout.findViewById(R.id.photo_text_bar);
		return layout;
	}
}
